package org.ufinity.hsms.module.setting;

import org.ufinity.hsms.R;
import org.ufinity.hsms.common.activity.BaseActivity;
import org.ufinity.hsms.common.application.HSMApplication;
import org.ufinity.hsms.common.utils.StringUtils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ServiceSettingActivity extends BaseActivity implements OnClickListener{
	public static final String TAG = "SettingActivity";
	private HSMApplication application;
	
	EditText edt_1;
	EditText edt_2;
	EditText edt_3;
	EditText edt_4;
	EditText edt_5;
	
	CheckBox cb_auth;
	LinearLayout lay_auth;
	Button btn_save;
	
	Animation animation;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		application = getBaseApplication();
		application.addActivity(instance);
		setContentView(R.layout.setting_service);
		animation = AnimationUtils.loadAnimation(this, R.anim.shake);
		initView();
		initDate();
	}

	private void initDate() {
		SharedPreferences sharedPreferences = this.getSharedPreferences("service_settings", Activity.MODE_PRIVATE);
		if(!StringUtils.isBlank(sharedPreferences.getString("user_name2", ""))){
			edt_1.setText(sharedPreferences.getString("service_name", ""));
			edt_2.setText(sharedPreferences.getString("ip_address", ""));
			edt_3.setText(sharedPreferences.getString("user_name2", ""));
			edt_4.setText(sharedPreferences.getString("password2", ""));
			edt_5.setText(sharedPreferences.getString("out_time", ""));
			if(sharedPreferences.getBoolean("auth", false)){
				lay_auth.setVisibility(View.VISIBLE);
			}else{
				lay_auth.setVisibility(View.GONE);
			}
		}
	}

	private void initView() {
		edt_1 = (EditText) findViewById(R.id.edt_1);
		edt_1.setOnClickListener(this);
		edt_2 = (EditText) findViewById(R.id.edt_2);
		edt_2.setOnClickListener(this);
		edt_3 = (EditText) findViewById(R.id.edt_3);
		edt_3.setOnClickListener(this);
		edt_4 = (EditText) findViewById(R.id.edt_4);
		edt_4.setOnClickListener(this);
		edt_5 = (EditText) findViewById(R.id.edt_5);
		edt_5.setOnClickListener(this);
		
		cb_auth = (CheckBox) findViewById(R.id.cb_auth);
		cb_auth.setOnClickListener(this);
		lay_auth = (LinearLayout) findViewById(R.id.lay_auth);
		
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_save.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == cb_auth.getId()){
			if(cb_auth.isChecked()){
				lay_auth.setVisibility(View.VISIBLE);
			}else{
				lay_auth.setVisibility(View.GONE);
			}
		}else if(id == btn_save.getId()){
			save();
		}
	}

	private void save() {
		String service_name = edt_1.getText().toString().trim(); 
		String ip_address = edt_2.getText().toString().trim();
		String user_name2 = edt_3.getText().toString().trim();
		String password2 = edt_4.getText().toString().trim();
		String out_time = edt_5.getText().toString().trim();
		if (StringUtils.isBlank(service_name)){
			edt_1.startAnimation(animation);
			return;
		}
		
		if (StringUtils.isBlank(ip_address)){
			edt_2.startAnimation(animation);
			return;
		}
		
		if (StringUtils.isBlank(user_name2)){
			edt_3.startAnimation(animation);
			return;
		}
		
		if (StringUtils.isBlank(password2)){
			edt_4.startAnimation(animation);
			return;
		}
		
		if (StringUtils.isBlank(out_time)){
			edt_5.startAnimation(animation);
			return;
		}
		
		SharedPreferences sharedPreference = this.getSharedPreferences("service_settings", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreference.edit();
		editor.putString("service_name", service_name);
		editor.putString("ip_address", ip_address);
		editor.putString("user_name2", user_name2);
		editor.putString("password2", password2);
		editor.putString("out_time", out_time);
		editor.putBoolean("auth", cb_auth.isChecked());
		editor.commit();
		
		makeMyToast(instance, getResources().getString(R.string.success));
	}
}
